package com.example.moveclock.activities;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.Menu;
import android.widget.AnalogClock;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.example.moveclock.common.TimeHandler;
import com.example.moveclock.views.CircuitOfJoystickView;
import com.example.moveclock.views.JoystickView;

public class JoystickControlGameActivity extends Activity {
	RelativeLayout rlayout;
	CircuitOfJoystickView circuit;
	JoystickView joystick;

	private TimeHandler mHandler;

	private Timer timer = new Timer();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_joystick_control_game);
		rlayout = (RelativeLayout) findViewById(R.id.rLayout);
		addJoystickOnView();

		mHandler = new TimeHandler();
		mHandler.setClock((AnalogClock) findViewById(R.id.analogClock1));
		mHandler.setJoystick(joystick);
		mHandler.setRlayout(rlayout);
		this.startTimer();
	}

	protected void startTimer() {
		timer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				mHandler.sendMessage(new Message());

			}
		}, 0, 10);
	}

	private void addJoystickOnView() {
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.addRule((int) RelativeLayout.ALIGN_PARENT_BOTTOM);
		params.addRule((int) RelativeLayout.ALIGN_PARENT_RIGHT);
		params.setMargins(0, 0, 20, 20);

		circuit = new CircuitOfJoystickView(this);
		circuit.setLayoutParams(params);
		rlayout.addView(circuit);

		joystick = new JoystickView(this);
		joystick.setLayoutParams(params);
		rlayout.addView(joystick);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_joystick_control_game, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		timer.cancel();
		super.onDestroy();
	}

}
